package Controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"hr-system/Global"
	"hr-system/Model"
	response "hr-system/Response"
	"time"
)

type IDepartmentTransferController interface {
	RestController
}

type DepartmentTransferController struct {
	DB *gorm.DB
}

func NewDepartmentTransferController() IDepartmentTransferController{
	db := Global.GetDB()
	return DepartmentTransferController{DB: db}
}

func (d DepartmentTransferController) PageList(c *gin.Context) {
	var requestEmployee Model.Employee
	_ = c.ShouldBind(&requestEmployee)
	name := requestEmployee.Name
	if name != ""{
		var selectEmployee []Model.DepartmentTransferEmployee
		// 查询
		d.DB.Model(&selectEmployee).Where("name=?", name).Find(&selectEmployee)
		if len(selectEmployee) == 0 {
			response.Fail(c, "查无相关记录", nil)
			return
		}
	}
	var record []Model.DepartmentTransferEmployee
	d.DB.Order("id").Find(&record)
	length := len(record)
	re := make([]Model.ReturnDepartmentTransfer, 0)
	for i:=0; i<length; i++ {
		var tempE Model.Employee
		d.DB.Where("id=?",record[i].Id).First(&tempE)
		if name != "" && (name != tempE.Name) {
			continue
		}
		var tempOD Model.Department
		d.DB.Where("department_number=?",record[i].OriginDepartmentNumber).First(&tempOD)
		var tempND Model.Department
		d.DB.Where("department_number=?",record[i].NewDepartmentNumber).First(&tempND)
		re = append(re, Model.ReturnDepartmentTransfer{
			Id:record[i].Id,
			Name:tempE.Name,
			Sex:tempE.Sex,
			OriginDepartmentName:tempOD.DepartmentName,
			NewDepartmentName:tempND.DepartmentName,
			TransferDate:record[i].TransferDate.Format("2006-01-02"),
			ReasonsForTransfer:record[i].ReasonsForTransfer,
		})
	}

	response.Success(c, gin.H{"data": re}, "查询成功")
}

func (d DepartmentTransferController) Create(c *gin.Context) {
	var requestEmployee Model.NewDepartmentTransfer
	_ = c.ShouldBind(&requestEmployee)
	updateEmployee := Model.Employee{}
	result := d.DB.Where("id=?",requestEmployee.Id).First(&updateEmployee).RowsAffected
	if result == 0 {
		response.Fail(c, "该员工不存在，无法调动", nil)
		return
	}
	if updateEmployee.IsQuit == "是" {
		response.Fail(c, "该员工已离职", nil)
		return
	}
	var total int64
	d.DB.Model(Model.DepartmentTransferEmployee{}).Count(&total)
	var tempD Model.Department
	var tempP Model.Post
	d.DB.Where("department_name=?",requestEmployee.NewDepartmentName).First(&tempD)
	d.DB.Where("post_name=?",requestEmployee.NewDepartmentName).First(&tempP)
	t,_ := time.Parse("2006-01-02", requestEmployee.TransferDate)
	newRecord := Model.DepartmentTransferEmployee{
		TransferId: int(total+1),
		Id: requestEmployee.Id,
		OriginDepartmentNumber: updateEmployee.DepartmentNumber,
		NewDepartmentNumber: tempD.DepartmentNumber,
		OriginPostNumber: updateEmployee.PostNumber,
		NewPostNumber: tempP.PostNumber,
		TransferDate: t,
		ReasonsForTransfer: requestEmployee.ReasonsForTransfer,
	}
	if newRecord.NewDepartmentNumber == newRecord.OriginDepartmentNumber {
		response.Fail(c, "原部门和新部门相同", nil)
		return
	}
	err := d.DB.Create(&newRecord).Error
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	d.DB.Model(&updateEmployee).Updates(map[string]interface{}{"department_number":newRecord.NewDepartmentNumber, "post_number": newRecord.NewPostNumber})
	if err != nil {
		response.Fail(c, err.Error(), nil)
		return
	}
	response.Success(c, gin.H{"data": requestEmployee}, "调动成功")
}
